package com.estore.test;

import com.estore.model.User;
import com.estore.service.UserService;

/**
 * 积分系统测试类
 * 测试积分的增加、扣除、VIP升级/降级功能
 */
public class PointsSystemTest {
    
    public static void main(String[] args) {
        System.out.println("=== 积分系统测试开始 ===");
        
        UserService userService = new UserService();
        
        // 测试1: 创建新用户
        System.out.println("\n1. 测试创建新用户");
        User testUser = userService.register("testuser", "123456", "test@example.com");
        System.out.println("创建用户: " + testUser);
        System.out.println("初始积分: " + testUser.getPoints());
        System.out.println("初始角色: " + testUser.getRole());
        
        // 测试2: 增加积分（模拟消费）
        System.out.println("\n2. 测试增加积分");
        User updatedUser = userService.addPointsByAmount(testUser.getUserId(), 5000); // 消费5000元，获得500积分
        System.out.println("消费5000元后积分: " + updatedUser.getPoints());
        System.out.println("角色: " + updatedUser.getRole());
        
        // 测试3: 继续增加积分直到VIP升级
        System.out.println("\n3. 测试VIP升级");
        updatedUser = userService.addPointsByAmount(testUser.getUserId(), 6000); // 再消费6000元，获得600积分
        System.out.println("再消费6000元后积分: " + updatedUser.getPoints());
        System.out.println("角色: " + updatedUser.getRole());
        System.out.println("是否升级为VIP: " + (updatedUser.getRole() == User.UserRole.VIP ? "是" : "否"));
        
        // 测试4: 扣除积分（模拟退款）
        System.out.println("\n4. 测试扣除积分");
        User beforeDeduct = userService.findUserById(testUser.getUserId());
        System.out.println("扣除前积分: " + beforeDeduct.getPoints());
        
        User afterDeduct = userService.deductPointsByAmount(testUser.getUserId(), 3000); // 扣除300积分
        System.out.println("扣除300积分后: " + afterDeduct.getPoints());
        System.out.println("角色: " + afterDeduct.getRole());
        
        // 测试5: 大量扣除积分测试VIP降级
        System.out.println("\n5. 测试VIP降级");
        updatedUser = userService.deductPointsByAmount(testUser.getUserId(), 20000); // 扣除2000积分
        System.out.println("大量扣除积分后: " + updatedUser.getPoints());
        System.out.println("角色: " + updatedUser.getRole());
        System.out.println("是否降级为普通用户: " + (updatedUser.getRole() == User.UserRole.CUSTOMER ? "是" : "否"));
        
        // 测试6: 积分不能为负数
        System.out.println("\n6. 测试积分边界值");
        User beforeBoundary = userService.findUserById(testUser.getUserId());
        System.out.println("边界测试前积分: " + beforeBoundary.getPoints());
        
        User afterBoundary = userService.updatePoints(testUser.getUserId(), -10000); // 尝试扣除大量积分
        System.out.println("尝试扣除10000积分后: " + afterBoundary.getPoints());
        System.out.println("积分是否为负数: " + (afterBoundary.getPoints() < 0 ? "是" : "否"));
        
        // 测试7: 管理员调整积分
        System.out.println("\n7. 测试管理员调整积分");
        User adminUser = userService.findUserById("admin");
        if (adminUser != null) {
            System.out.println("管理员用户: " + adminUser);
            
            // 给测试用户增加积分
            User adjustedUser = userService.updatePoints(testUser.getUserId(), 1500);
            System.out.println("管理员调整+1500积分后: " + adjustedUser.getPoints());
            System.out.println("角色: " + adjustedUser.getRole());
        }
        
        System.out.println("\n=== 积分系统测试完成 ===");
        System.out.println("所有功能正常工作！");
    }
}